'********************************************************************
' 
'  file:  PurchasingManagementSample.vb
'  PurchasingManagement Sample Call
' 
'  (C) Copyright 2010, Diomede Corporation
'  All rights reserved
' 
'  Use, modification, and distribution is subject to   
'  the New BSD License (See accompanying file license.txt).
' 
'********************************************************************

Imports System
Imports data.diomedestorage.com
Imports Diomede.SDK.Enums

Namespace Diomede.SDK.ExampleVB
    Class PurchasingManagementSample
        Shared username As String = String.Format("user_{0}", DateTime.Now.Ticks)
        Shared password As String = "password914"
        Shared email As String = String.Format("{0}@example.com", username)

        Public Sub Start()
            Try
                CreateUser()

                Console.WriteLine("" & Chr(10) & "" & Chr(10) & "")
                GetAllProductsSample()
                PuchaseProductsSample()
                GetMyProductsSample()
                CancelProductsSample()
                GetAllContractsSample()
                PuchaseContractsSample()
                GetMyContractsSample()
                CancelContractsSample()
                'GetAllSupportsSample()
                'PuchaseSupportsSample()
                'GetMySupportsSample()
                'CancelSupportsSample()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Utils.WriteMessage("Press any key...")
            Console.ReadKey()

        End Sub

        Private Shared Sub CreateUser()
            Dim session As ISession = New Session()
            Dim accountManager As IAccountManagement = TryCast(session, IAccountManagement)
            If accountManager.Create(username, password, email).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to create new user.")
            End If
        End Sub

        Private Shared Sub LoginSampleUser(ByVal session As ISession)
            If session.Login(username, password).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to login.")
            End If
        End Sub

#Region "Examples"

#Region "Products"
        Public Sub GetAllProductsSample()
            Dim session As ISession = New Session()
            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
            Dim result As ProductsExecResult = pm.GetAllProducts()

            If result.Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to get all products.")
            Else
                Console.WriteLine("All products successfully retrieved.")
            End If

        End Sub

        Public Sub PuchaseProductsSample()
            Dim session As ISession = New Session()

            LoginSampleUser(session)

            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
            Dim getproducts As ProductsExecResult = pm.GetAllProducts()

            Dim productToPurchase As Product = getproducts.Products(0)

            If pm.PurchaseProduct(productToPurchase.productID).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to purchase products.")
            Else
                Console.WriteLine("Product succesfully purchased.")
            End If

        End Sub

        Public Sub GetMyProductsSample()
            Dim session As ISession = New Session()

            LoginSampleUser(session)

            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
            Dim getproducts As ProductsExecResult = pm.GetAllProducts()

            Dim productToPurchase As Product = getproducts.Products(0)

            Dim purchasePrudResult As ProductsExecResult = pm.PurchaseProduct(productToPurchase.productID)

            If pm.GetMyProducts().Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to retrieve my products.")
            Else
                Console.WriteLine("My Product successfully retrieved.")
            End If
        End Sub

        Public Sub CancelProductsSample()
            Dim session As ISession = New Session()

            LoginSampleUser(session)

            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
            Dim getproducts As ProductsExecResult = pm.GetAllProducts()

            Dim productToPurchase As Product = getproducts.Products(0)

            Dim purchasePrudResult As ProductsExecResult = pm.PurchaseProduct(productToPurchase.productID)

            If pm.CancelProduct(pm.GetMyProducts().UserProducts(0).product.productID).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to cancel products.")
            Else
                Console.WriteLine("My Product successfully cancelled.")
            End If

        End Sub

#End Region

#Region "Contracts"
        Public Sub GetAllContractsSample()
            Dim session As ISession = New Session()
            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
            Dim result As ContractsExecResult = pm.GetAllContracts()

            If result.Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to get all Contracts.")
            Else
                Console.WriteLine("All Contracts successfully retrieved.")
            End If

        End Sub

        Public Sub PuchaseContractsSample()
            Dim session As ISession = New Session()

            LoginSampleUser(session)

            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
            Dim getContracts As ContractsExecResult = pm.GetAllContracts()

            Dim contractToPurchase As Contract = getContracts.Contracts(0)

            If pm.PurchaseContract(contractToPurchase.ContractID).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to purchase Contracts.")
            Else
                Console.WriteLine("contract succesfully purchased.")
            End If

        End Sub

        Public Sub GetMyContractsSample()
            Dim session As ISession = New Session()

            LoginSampleUser(session)

            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
            Dim getContracts As ContractsExecResult = pm.GetAllContracts()

            Dim contractToPurchase As Contract = getContracts.Contracts(0)

            Dim purchasePrudResult As ContractsExecResult = pm.PurchaseContract(contractToPurchase.ContractID)

            If pm.GetMyContracts().Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to retrieve my Contracts.")
            Else
                Console.WriteLine("My contract successfully retrieved.")
            End If
        End Sub

        Public Sub CancelContractsSample()
            Dim session As ISession = New Session()

            LoginSampleUser(session)

            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
            Dim getContracts As ContractsExecResult = pm.GetAllContracts()

            Dim contractToPurchase As Contract = getContracts.Contracts(0)

            Dim purchasePrudResult As ContractsExecResult = pm.PurchaseContract(contractToPurchase.ContractID)

            If pm.CancelContract(pm.GetMyContracts().UserContracts(0).contract.contractID).Status <> ExecutionResults.Success Then
                Throw New Exception("Fails to cancel Contracts.")
            Else
                Console.WriteLine("My contract successfully cancelled.")
            End If

        End Sub

#End Region

        '#Region "Supports"
        '        Public Sub GetAllSupportsSample()
        '            Dim session As ISession = New Session()
        '            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
        '            Dim result As SupportsExecResult = pm.GetAllSupports()

        '            If result.Status <> ExecutionResults.Success Then
        '                Throw New Exception("Fails to get all Supports.")
        '            Else
        '                Console.WriteLine("All Supports successfully retrieved.")
        '            End If

        '        End Sub

        '        Public Sub PuchaseSupportsSample()
        '            Dim session As ISession = New Session()

        '            LoginSampleUser(session)

        '            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
        '            Dim getSupports As SupportsExecResult = pm.GetAllSupports()

        '            Dim supportToPurchase As Support = getSupports.Supports(0)

        '            If pm.PurchaseContract(supportToPurchase.supportID).Status <> ExecutionResults.Success Then
        '                Throw New Exception("Fails to purchase Supports.")
        '            Else
        '                Console.WriteLine("contract succesfully purchased.")
        '            End If

        '        End Sub

        '        Public Sub GetMySupportsSample()
        '            Dim session As ISession = New Session()

        '            LoginSampleUser(session)

        '            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
        '            Dim getSupports As SupportsExecResult = pm.GetAllSupports()

        '            Dim supportToPurchase As Support = getSupports.Supports(0)

        '            Dim purchasePrudResult As SupportsExecResult = pm.PurchaseSupport(supportToPurchase.supportID)

        '            If pm.GetMySupports().Status <> ExecutionResults.Success Then
        '                Throw New Exception("Fails to retrieve my Supports.")
        '            Else
        '                Console.WriteLine("My contract successfully retrieved.")
        '            End If
        '        End Sub

        '        Public Sub CancelSupportsSample()
        '            Dim session As ISession = New Session()

        '            LoginSampleUser(session)

        '            Dim pm As IPurchasingManagement = TryCast(session, IPurchasingManagement)
        '            Dim getSupports As SupportsExecResult = pm.GetAllSupports()

        '            Dim supportToPurchase As Support = getSupports.Supports(0)

        '            Dim purchasePrudResult As SupportsExecResult = pm.PurchaseSupport(supportToPurchase.supportID)

        '            If pm.CancelContract(pm.GetMySupports().UserSupports(0).userSupportID).Status <> ExecutionResults.Success Then
        '                Throw New Exception("Fails to cancel Supports.")
        '            Else
        '                Console.WriteLine("My contract successfully cancelled.")
        '            End If

        '        End Sub

        '#End Region

#End Region
    End Class
End Namespace